package com.vanzo.talkie;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.vanzo.bean.Follower;
import com.vanzo.bean.FollowerDaoUtil;
import com.vanzo.talkie2.R;
import com.vanzo.util.LogUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by tisen on 2018/1/31.
 */

public class FollowerAdapter extends RecyclerView.Adapter<FollowerAdapter.FollowerItem> {

    private Activity activity;
    private List<Follower> followers = new ArrayList<>();
    private ItemClickListener clickListener;
    private FollowerDaoUtil followerDaoUtil;
    private View recycler;
    private View add;


    public void setClickListener(ItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setRecyclerAndAdd(View recycler, View add) {
        this.recycler = recycler;
        this.add = add;
    }

    public FollowerAdapter(Activity activity) {
        this.activity = activity;
        this.followerDaoUtil = new FollowerDaoUtil(activity);
        this.followers = followerDaoUtil.queryAllFollower();
    }

    @Override
    public FollowerItem onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FollowerItem(LayoutInflater.from(parent.getContext()).inflate(R.layout.follower_item, parent, false));
    }

    @Override
    public void onBindViewHolder(FollowerItem holder, int position) {
        holder.bind(followers.get(position));


    }

    @Override
    public int getItemCount() {
        return followers.size();
    }

    public void updateFollowers() {
        this.followers = followerDaoUtil.queryAllFollower();
        notifyDataSetChanged();
//        recycler.setVisibility(followers.isEmpty() ? View.INVISIBLE : View.VISIBLE);
//        add.setVisibility(!followers.isEmpty() ? View.INVISIBLE : View.VISIBLE);
    }

    class FollowerItem extends RecyclerView.ViewHolder {
        private ImageView avatar;
        private TextView code;
        private Follower follower;

        public FollowerItem(View itemView) {
            super(itemView);
            avatar = (ImageView) itemView.findViewById(R.id.follower_avatar);
            code = (TextView) itemView.findViewById(R.id.follower_code);
        }

        public void bind(Follower mFollower) {
            this.follower = mFollower;
            if (mFollower.getKilKey() != null) {
                avatar.setImageResource(follower.getKillStatus() ? R.drawable.avatar_killed : R.drawable.avatar_normal);
                String codeString = follower.getTerminalId() + "";
                code.setText(codeString.length() < 2 ? "0" + codeString : codeString);

                avatar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        follower.setKillStatus(!follower.getKillStatus());
                        followerDaoUtil.updateFollower(follower);
                        updateFollowers();
                        if (clickListener != null) {
                            clickListener.onClick(follower);
                        }
                    }
                });


                avatar.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        if (clickListener != null) {
                            clickListener.onLongClick(follower);
                        }
                        return false;
                    }
                });
            } else {
                avatar.setImageResource(R.drawable.avatar_none);
                avatar.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(activity, "未绑定从机", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    }

    interface ItemClickListener {
        void onClick(Follower follower);

        void onLongClick(Follower follower);
    }
}
